/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package maxent.mr.mapred;

import java.io.IOException;
import java.util.HashMap;
import maxent.mr.writable.TagCountWritable;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;



/**
 *
 * @author hadoop
 */
public class DictionaryReducer
        extends Reducer<Text, Text, Text, TagCountWritable>{

    private HashMap<Text, IntWritable> map;

    @Override
    public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException{

        map = new HashMap<Text, IntWritable>();

        for(Text value : values){            
            add(value);
        }

        context.write(key, new TagCountWritable(map));
                
    }

    private void add(Text tag) {
        IntWritable val = new IntWritable();

        if (map.get(tag) != null) {
          val = map.get(tag);
        } else {
          val.set(0);
        }

        map.put(tag, new IntWritable(val.get()+1));
    }
}
